<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Redirect, Input;
use App\Land;
use App\Landowner;
use App\Contract;
use Illuminate\Support\Facades\DB;

class contractController extends Controller
{
     public function __construct()
    {
        $this->middleware('auth');
    }

    //
	public function index()  
	{
		$contracts = DB::table('contracts')
		->join('landowners', 'contracts.landowner_id', '=', 'landowners.id')
		->select('contracts.*', 'landowners.name')
		->get();
		return view('contract.index')->withContracts($contracts);
                  // return response()->json($contracts); 
		
	}

	//
	public function showByLandowner($landowner_id)  
	{
		$contracts = DB::table('contracts')
		->where('contracts.landowner_id', $landowner_id)
		->join('landowners', 'contracts.landowner_id', '=', 'landowners.id')
		->select('contracts.*', 'landowners.name')
		->get();
		return view('contract.index')->withContracts($contracts);
                  // return response()->json($contracts); 
		
	}

   	public function show($id)
    {
        $landowner = DB::table('contracts')->where('id', $id)->first();
        return view('landowner.detail')->withLandowner($landowner);
            // return response()->json($landowner); 
    }


    public function modify($id)
    {
    	$landowners = Landowner::all();

        $contract = DB::table('contracts')->where('id', $id)->first();
    
        return view('contract.modify', ["contract"=>$contract,"landowners"=>$landowners]);
        // return response()->json($landowner); 
    }

	

	public function store(Request $request)
	{
		$this->validate($request, [
			'landowner_id' => 'required',
			'code' => 'required|unique:contracts',
			]);

		$contract = new Contract;
		$contract->landowner_id = $request->input("landowner_id");
		$contract->code = $request->input("code");
		$contract->start_date = $request->input("start_date");
		$contract->peroid = $request->input("peroid");
		$contract->amount = $request->input("amount");
		$contract->comment = $request->input("comment");

		
		if ($contract->save()) {
			return Redirect::to('contract');
		} else {
			return Redirect::back()->withInput()->withErrors('保存失败！');
		}

	}

	public function update(Request $request)
    {
        $this->validate($request, [
            'landowner_id' => 'required',
            'id' => 'required',
        ]);

   
        $contact = [
        "landowner_id"=>$request->input("landowner_id"),
        "code"=>$request->input("code"),
        "start_date"=>$request->input("start_date"),
        "peroid"=>$request->input("peroid"),
        "amount"=>$request->input("amount"),
        "comment"=>$request->input("comment")
        ];
    
        DB::table('contracts')
            ->where('id', $request->input("id"))
            ->update($contact);
      
        return Redirect::to('contract');
    }
    
	public function create()
	{
		$landowners = Landowner::all();
		return view('contract.create')->withLandowners($landowners);
	}

	public function delete($id)
	{
		DB::table('contracts')->where('id', $id)->delete();
		
		return Redirect::to('contract');
	}

}
